// https://leetcode.cn/problems/find-closest-lcci/
/*
有个内含单词的超大文本文件，给定任意两个不同的单词，找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次，而每次寻找的单词不同，你能对此优化吗?

输入：words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
输出：1



*/

#include <iostream>
#include <vector>
#include <string>

using namespace std;


class Solution {
public:
    int findClosest(vector<string>& words, string word1, string word2) {
		string leftS = "";
		int left = 0;
		int len = words.size();
		
		int min = 10000;
		int i;
		for(i = 0; i < len; i++){
			if(word1 == words[i] || word2 == words[i]){
				leftS = words[i];
				left = i;
				break;
			}
		}
		for(int j = i; j < len; j++){
			if(word1 == words[j] || word2 == words[j]){
				if(leftS != words[j]){
					if((j - left) < min){
						min = j - left;
					}
				}
				leftS = words[j];
				left = j;
			}
		}
		return min;
    }
};


int main(){
	vector<string> a = {"vg","ti","te","yd","ja","vqx","a"};
	string w1 = "vg";
	string w2 = "te";
	Solution so;
	int min = so.findClosest(a, w1, w2);
	cout << min << endl;
	return 0;
}